package com.coincalf.wallet.blockchain.util;

import com.coincalf.wallet.blockchain.BlockConstant;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * 加密解密工具类
 */
public class AESUtil {

    private static final String KEY_AES = "AES";

    public static String encrypt(String src, String key) {
        if (key == null /*|| key.length() != 16*/) {
            throw new RuntimeException("key不满足条件");
        }
        byte[] raw = key.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_AES);
        byte[] encrypted;
        try {
            Cipher cipher = Cipher.getInstance(KEY_AES);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            encrypted = cipher.doFinal(src.getBytes());
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | InvalidKeyException | BadPaddingException e) {
            throw new RuntimeException("AES ENCRYPT ERROR");
        }
        return byte2hex(encrypted);
    }

    public static String decrypt(String src, String key) {
        if (key == null /*|| key.length() != 16*/) {
            throw new RuntimeException("key不满足条件");
        }
        byte[] raw = key.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_AES);

        byte[] original;
        try {
            Cipher cipher = Cipher.getInstance(KEY_AES);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = hex2byte(src);
            original = cipher.doFinal(encrypted1);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
            throw new RuntimeException("AES DECRYPT ERROR");
        }
        return new String(original);
    }

    public static byte[] hex2byte(String strhex) {
        if (strhex == null) {
            return null;
        }
        int l = strhex.length();
        if (l % 2 == 1) {
            return null;
        }
        byte[] b = new byte[l / 2];
        for (int i = 0; i != l / 2; i++) {
            b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
                    16);
        }
        return b;
    }

    public static String byte2hex(byte[] b) {
        StringBuilder hs = new StringBuilder();
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs.append("0").append(stmp);
            } else {
                hs.append(stmp);
            }
        }
        return hs.toString().toUpperCase();
    }

    public static void main(String[] args) throws Exception {
        String content = "7ae7dd7cca2b0847fed67519bd594de95dba2f5972c7437b18cd3f906a93684d";
        System.out.println("原内容 = " + content);
        String decrypt = AESUtil.encrypt(content, BlockConstant.AES_KEY);
        System.out.println("加密后 = " + decrypt);
        String aa = "D31912FABD50ABDE2B6A1DAD80D1E226832145A05C7F7DF395F50A40443554FCD7ABA7E6C475B10F65A444904460AC79C10C03D01833133ABF4096033B804C9E1AAC60F54F5DDC2B4C6F24B6C4B545C3";
        System.out.println(AESUtil.decrypt(aa, BlockConstant.AES_KEY));
        //0x191A2d9995032Ce780696aBa8308d32206c4270d
    }

}
